package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.mapper.UserProductPreferenceMapper;
import com.qys.livesMall.shop.modal.entity.UserProductPreference;
import com.qys.livesMall.shop.service.UserProductPreferenceService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 用户商品偏好表(UserProductPreference)表服务实现类
 *
 * @author makejava
 * @since 2023-08-23 18:10:54
 */
@Service("userProductPreferenceService")
public class UserProductPreferenceServiceImpl extends ServiceImpl<UserProductPreferenceMapper, UserProductPreference> implements UserProductPreferenceService {

    @Resource
    private UserProductPreferenceMapper userProductPreferenceMapper;


    @Override
    public void updatePreference(Long productCategoryId) {
        UserProductPreference byUserId = this.getByUserId(SecurityUtils.getUserId(), productCategoryId);
        if(byUserId == null){
            UserProductPreference userProductPreference = new UserProductPreference();
            userProductPreference.setUserId(SecurityUtils.getUserId());
            userProductPreference.setProductCategoryId(productCategoryId);
            userProductPreference.setViews(1);
            save(userProductPreference);
        } else {
            userProductPreferenceMapper.updateViews(byUserId.getUserId(), byUserId.getProductCategoryId());
        }
    }

    @Override
    public UserProductPreference getByUserId(Long userId, Long productCategoryId) {
        return getOne(Wrappers.<UserProductPreference>lambdaQuery().eq(UserProductPreference::getUserId, userId).eq(UserProductPreference::getProductCategoryId, productCategoryId));
    }
}

